﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using Newtonsoft.Json.Linq;
using System.Data;
using Senparc.Weixin.QY.Entities;
using Senparc.Weixin.QY.AdvancedAPIs.Mass;
using Senparc.Weixin.QY.CommonAPIs;
using Senparc.Weixin.QY.AdvancedAPIs.MailList;
using Senparc.Weixin.Entities;
using Senparc.Weixin.QY.AdvancedAPIs.OAuth2;
using System.IO;
using System.Text;
using SZHL.Data;

namespace SZHL.UI.AppCode
{
    public class WXHelp
    {

        public static string WXUrl = WXHelp.GetWXInfo("WXUrl");
        //public static string corpId = WXHelp.GetWXInfo("corpId");
        public static string corpId = "wxf285796b249c0cf6";
        //public static string corpSecret = WXHelp.GetWXInfo("corpSecret");
        public static string corpSecret = "0XSmLBYtwGg0W1lDnYA2cdY_GxA1e_uTd_m-vMkbODvTH1-6gbFIAbUCo3sq77ET";
        public static string appTZGGID = WXHelp.GetWXInfo("appTZGGID");//通知公告应用ID
        public static string appGRXXID = WXHelp.GetWXInfo("appGRXXID");//个人消息ID


        //public static string isUseWX = WXHelp.GetWXInfo("isUseWX");//是否使用企业号的开关
        public static string isUseWX = "Y";//是否使用企业号的开关

        public static string GetToken()
        {
            if (isUseWX == "Y")
            {
                AccessTokenResult Token = CommonApi.GetToken(WXHelp.corpId, WXHelp.corpSecret);
                return Token.access_token;
            }
            else
            {
                return "";
            }
        }
        public static JsApiTicketResult GetTicket()
        {
            if (isUseWX == "Y")
            {
                JsApiTicketResult js = CommonApi.GetTicket(WXHelp.corpId, WXHelp.corpSecret);
                return js;
            }
            else
            {
                return null;

            }
        }
        //发送通知消息


        public void SendWXMsg(Article MODEL, string strAPPID, string strUserS = "@all")
        {
            if (isUseWX == "Y")
            {
                List<Article> Msgs = new List<Article>();
                Msgs.Add(MODEL);
                MassApi.SendNews(WXHelp.GetToken(), strUserS, "", "", strAPPID, Msgs);
            }
        }
        public void SendWXRText(string MsgText, string strAPPID, string strUserS = "@all")
        {
            if (isUseWX == "Y")
            {

                MassApi.SendText(WXHelp.GetToken(), strUserS, "", "", strAPPID, MsgText);
            }
        }
        public string GetCodeURL(string Redurl)
        {
            string url = "";
            if (isUseWX == "Y")
            {
                url = OAuth2Api.GetCode(WXHelp.corpId, Redurl, "");
            }
            return url;
        }
        public string GetUserDataByCode(string strCode, int AppID)
        {
            string UserCode = "";

            try
            {
                if (isUseWX == "Y")
                {

                    GetUserIdResult OBJ = OAuth2Api.GetUserId(WXHelp.GetToken(), strCode, AppID);
                    UserCode = OBJ.UserId;

                }
            }
            catch (Exception EX)
            {
                new JH_Auth_LogB().Insert(new JH_Auth_Log() { CRDate = DateTime.Now, LogContent = "获取用户代码GetUserDataByCode" + EX.Message.ToString() });

            }

            return UserCode;
        }

        public static class BranchMangner
        {




            public static void WX_CreateBranch(JH_Auth_Branch Model)
            {
                if (isUseWX == "Y")
                {
                    MailListApi.CreateDepartment(WXHelp.GetToken(), Model.DeptName, Model.DeptRoot, Model.DeptShort, Model.DeptCode);
                }
            }

            public static QyJsonResult WX_UpdateBranch(JH_Auth_Branch Model)
            {
                QyJsonResult Ret = new QyJsonResult();
                if (isUseWX == "Y")
                {
                    Ret = MailListApi.UpdateDepartment(WXHelp.GetToken(), Model.DeptCode.ToString(), Model.DeptName, Model.DeptRoot, Model.DeptShort);
                }
                return Ret;
            }

            public static QyJsonResult WX_DelBranch(string strDeptCode)
            {
                QyJsonResult Ret = new QyJsonResult();
                if (isUseWX == "Y")
                {
                    Ret = MailListApi.DeleteDepartment(WXHelp.GetToken(), strDeptCode);
                }
                return Ret;
            }


            public static QyJsonResult WX_CreateUser(JH_Auth_User Model)
            {
                QyJsonResult Ret = new QyJsonResult();
                if (isUseWX == "Y")
                {
                    int[] Branch = { Model.BranchCode };
                    Ret = MailListApi.CreateMember(WXHelp.GetToken(), Model.UserName, Model.UserRealName, Branch, Model.zhiwu, Model.mobphone, Model.mailbox, Model.weixinnum);
                }
                return Ret;
            }
            public static QyJsonResult WX_UpdateUser(JH_Auth_User Model)
            {
                QyJsonResult Ret = new QyJsonResult();
                if (isUseWX == "Y")
                {
                    int[] Branch = { Model.BranchCode };
                    Ret = MailListApi.UpdateMember(WXHelp.GetToken(), Model.UserName, Model.UserRealName, Branch, Model.zhiwu, Model.mobphone, Model.mailbox, Model.weixinnum);
                }
                return Ret;
            }
            public static QyJsonResult WX_DelUser(string strUserName)
            {
                QyJsonResult Ret = new QyJsonResult();
                if (isUseWX == "Y")
                {
                    Ret = MailListApi.DeleteMember(WXHelp.GetToken(), strUserName);
                }
                return Ret;
            }
        }



        public static string GetWXInfo(string strKey)
        {
            try
            {
                //string strPath = System.Web.HttpRuntime.AppDomainAppPath + "View\\SiteSet\\SiteSet.json";
                //StreamReader sr = new StreamReader(strPath, Encoding.Default);
                //string strJson = sr.ReadToEnd();
                //JObject jo = JObject.Parse(strJson);
                //var data = jo.Properties().Where(item => item.Name.ToString() == strKey).FirstOrDefault();
                //sr.Close();
                //return data.Value.ToString();
                return "";
            }
            catch (Exception)
            {

                throw;
            }


        }
    }
}